package com.briup.jz.service.impl;

import com.briup.jz.bean.Address;
import com.briup.jz.bean.AddressExample;
import com.briup.jz.bean.extend.AddressExtend;
import com.briup.jz.dao.AddressMapper;
import com.briup.jz.dao.extend.AddressExtendMapper;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private AddressExtendMapper addressExtendMapper;
//    更新或者插入地址信息
    @Override
    public void saveOrUpdate(Address address) throws CustomerException {

//        判断地址主键是否为控股
        if (address.getId()!=null){
            AddressExample example =new AddressExample();
                example.createCriteria().andIdEqualTo(address.getId());
                addressMapper.updateByExample(address,example);
            }else {
                addressMapper.insert(address);
            }

    }
//根据地址id主键删除
    @Override
    public void deleteById(Long id) throws CustomerException {
        Address address = addressMapper.selectByPrimaryKey(id);
        if (address==null){
            throw  new CustomerException("这条地址信息不存在");
        }
        addressMapper.deleteByPrimaryKey(id);

    }

    @Override
    public List<Address> query(Long id) {
        AddressExample example =new AddressExample();
        if (id!=null){
            example.createCriteria().andUserIdEqualTo(id);
        }
        return addressMapper.selectByExample(example);
    }

    @Override
    public List<AddressExtend> queryWithUser(Long id) {

        return     addressExtendMapper.queryWithUser(id);
    }
}
